<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Ewan
 *
 * @author dragon.hu 2015.8.7 add
 */
 
/**
 * AppID:10269
 * PacketID:59671
 * SignKey:SphiUkNfDdvRQqWJ
 * AppKey:1BVeXwjbHyAFrsmO
 * 前端使用SignKey，服务端使用AppKey
 */
require_once 'BaseChannel.php';

//Ewan平台服务器端接入
class Ewan implements BaseChannel
{
	function __construct(){
		
	}
	
	//lBVeXwjbHyAFrsmO
	function ios($data){
		$AppID = 10269;
		$AppKey = "lBVeXwjbHyAFrsmO";
		echo $this->payCallBackMethod($cpId, $apiKey, IOS);
	}
	
	function android($data){
		$AppID = 10269;
		$AppKey = "lBVeXwjbHyAFrsmO";
		echo $this->payCallBackMethod($AppID, $AppKey, ANDROID);
	}
	

	/**
	 * 支付回调通知。
	 * 注：
	 * 1.sdk server通过http post请求cp server的接口，cp server都要有一个响应，响应内容是payCallback函数的返回值
	 * $platform:平台名字
	 */
	public function payCallBackMethod($cpId, $apiKey, $platform){
	
		$responseData = $_REQUEST;
		$orderLog = new ChargeOrder();
		try{
			if($responseData!=null){
				$orderLog->log("Ewan", array("sign" => $responseData['sign'], "serverid" => $responseData['serverid'], "custominfo" => $responseData['custominfo'], 
						"openid " => $responseData['openid '], "ordernum" => $responseData['ordernum'], "status" => $responseData['status'], "paytype" => $responseData['paytype'], 
						"amount" => $responseData['amount'], "errdesc" => $responseData['errdesc'], "paytime" => $responseData['paytime']
				));
	
				//进行md5的验证过程
				$signSource = $responseData['serverid']."|".$responseData['custominfo']."|".$responseData['openid']."|".$responseData['ordernum']."|".$responseData['status']."|".
						$responseData['paytype']."|".$responseData['amount']."|".$responseData['errdesc']."|".$responseData['paytime']."|".$apiKey;
				$sign = md5($signSource);
				if($sign == $responseData['sign'])
				{
					if(1 == $responseData['status'])	//支付状态：1为成功，其他为失败
					{
						$status = STATUS_SUCCESS;
					}else
					{
						$status = STATUS_FAILD;
					}
					//数据库中money单位为元，此处amount单位为分
					$money = $responseData['amount']/100; //换算成元单位
					if(false == $orderLog->recordOrder($status, $money, $responseData['custominfo'], "Ewan", $platform, $responseData['ordernum']))
					{
						$orderLog->log("Ewan","amount:".$responseData['amount']." orderid:"
								.$responseData['custominfo']." platform:".$platform." failure");
						//写入数据库失败，要返回false
						$orderLog->log("Ewan",  "order:".$responseData['custominfo']." insert database failure \r\n");
						return 101;//其他未知错误，返回101
					}
					$orderLog->log("Ewan","amount:".$responseData['amount']." orderid:"
							.$responseData['custominfo']." platform:".$platform." success");
					return 1;//返回给sdk server的响应内容，返回1表示成功
						
				}else{
					$orderLog->log("Ewan",  " order:".$responseData['custominfo']." sign failure \r\n");
					return 100;//签名错误,返回100
				}
			}else{
				$orderLog->log("Ewan", " json_decode failure \r\n");
				throw new exception(' interface return error');
			}
		}catch(Exception $e){
			$orderLog->log("Ewan",  " unknow error \r\n");
			throw new exception($e->getMessage());
		}
		return 101;//其他未知错误，返回101
	}
}